﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Text;
using DomainDriver.CommonUtilities.Enums;
using DomainDriver.DomainModeling.DomainObjects;
using DomainDriver.DomainModeling.StorageManagers.DataSetStorage;
using DomainDriver.DomainModeling.StorageManagers.EntityFrameworkStorage;
using ObjectRow = DomainDriver.DomainModeling.Example.ExampleModelDataSet.SingletonDomainObjectsRow;

namespace DomainDriver.DomainModeling.Example.DomainObjects
{
    public partial class SingletonDomainObject : IEfEntity<Guid>, IDataSetSerializable<DataSet, DataRow>, IDataSetSerializable<ExampleModelDataSet, DataRow>, IDataSetSerializable<ExampleModelDataSet, ObjectRow>
    {
        #region Entity Framework CTP5 Serialization Code

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public Guid EF_Key
        {
            get { return m_Key; }
            set { m_Key = value; }
        }

        public Nullable<int> EF_SelectedEnumerationValue
        {
            get { return (Nullable<int>)m_SelectedEnumerationDomainObjectId; }
            set
            {
                if (value.HasValue)
                {
                    m_SelectedEnumerationDomainObjectId = (EnumerationValue)value;
                }
                else
                {
                    m_SelectedEnumerationDomainObjectId = null;
                }
            }
        }

        #endregion

        #region DataSet Serialization Methods

        public void Serialize(DataSet datasource, DataRow objectRecord)
        {
            string keyColumnName = ExampleModelDataSet.SchemaHelper.SingletonDomainObjects.KeyColumn.ColumnName;
            string selectedEnumerationValueColumnName = ExampleModelDataSet.SchemaHelper.SingletonDomainObjects.SelectedEnumerationKeyColumn.ColumnName;

            objectRecord[keyColumnName] = m_Key;
            DataSetSerializationUtilities.SerializeNullableField<EnumerationValue, int>(m_SelectedEnumerationDomainObjectId, objectRecord, ExampleModelDataSet.SchemaHelper.SingletonDomainObjects.SelectedEnumerationKeyColumn);
        }

        public void Deserialize(DataSet datasource, DataRow objectRecord)
        {
            string keyColumnName = ExampleModelDataSet.SchemaHelper.SingletonDomainObjects.KeyColumn.ColumnName;
            string selectedEnumerationValueColumnName = ExampleModelDataSet.SchemaHelper.SingletonDomainObjects.SelectedEnumerationKeyColumn.ColumnName;

            m_Key = objectRecord.Field<Guid>(keyColumnName);
            DataSetSerializationUtilities.DeserializeNullableField<EnumerationValue, int>(out m_SelectedEnumerationDomainObjectId, objectRecord, ExampleModelDataSet.SchemaHelper.SingletonDomainObjects.SelectedEnumerationKeyColumn);
        }

        #endregion

        #region ExampleModelDataSet Serialization Methods

        public string ObjectTableName
        {
            get { return ExampleModelDataSet.SchemaHelper.SingletonDomainObjects.TableName; }
        }

        public string KeyColumnName
        {
            get { return ExampleModelDataSet.SchemaHelper.SingletonDomainObjects.KeyColumn.ColumnName; }
        }

        public void Serialize(ExampleModelDataSet datasource, DataRow objectRecord)
        {
            Serialize(datasource, objectRecord as ObjectRow);
        }

        public void Serialize(ExampleModelDataSet datasource, ObjectRow objectRecord)
        {
            objectRecord.Key = m_Key;
            DataSetSerializationUtilities.SerializeNullableField<EnumerationValue, int>(m_SelectedEnumerationDomainObjectId, objectRecord, ExampleModelDataSet.SchemaHelper.SingletonDomainObjects.SelectedEnumerationKeyColumn);
        }

        public void Deserialize(ExampleModelDataSet datasource, DataRow objectRecord)
        {
            Deserialize(datasource, objectRecord as ObjectRow);
        }

        public void Deserialize(ExampleModelDataSet datasource, ObjectRow objectRecord)
        {
            m_Key = objectRecord.Key;
            DataSetSerializationUtilities.DeserializeNullableField<EnumerationValue, int>(out m_SelectedEnumerationDomainObjectId, objectRecord, ExampleModelDataSet.SchemaHelper.SingletonDomainObjects.SelectedEnumerationKeyColumn);
        }

        #endregion
    }
}